<?php

!defined('DEBUG') AND exit('Access Denied.');

$action = param(1);

// hook thread_start.php

// 发表主题帖 | create new thread
if ($action == 'create') {

	// hook thread_create_get_post.php

	user_login_check();

	if ($method == 'GET') {

		// hook thread_create_get_start.php

		$fid = param(2, 0);
		$sid = param(3, 0);
		$forum = $fid ? forum_read($fid) : array();
		$forum_sortlist = sort_find_by_fid($fid);
		$slist = sort_forum_list_cache();
		$forumlist_allowthread = forum_list_access_filter($forumlist, $gid, 'allowthread');
		$forumarr = xn_json_encode(arrlist_key_values($forumlist_allowthread, 'fid', 'name'));
		if (empty($forumlist_allowthread)) {
			message(-1, lang('user_group_insufficient_privilege'));
		}

		$arealist = area_read_all($u_city['cid']);

		$header['title'] = lang('create_thread');
		$header['mobile_title'] = $fid ? $forum['name'] : '';
		$header['mobile_linke'] = url("forum-$fid");

		// hook thread_create_get_end.php

		include _include(APP_PATH . 'view/htm/post.htm');

	} else {

		// hook thread_create_thread_start.php

		$fid = param('fid', 0);
		$sid = param('sid', 0);
		$cid = param('cid', 0);

		$forum = forum_read($fid);
		empty($forum) AND message('fid', lang('forum_not_exists'));

		$r = forum_access_user($fid, $gid, 'allowthread');
		!$r AND message(-1, lang('user_group_insufficient_privilege'));

		$subject = param('subject');
		empty($subject) AND message('subject', lang('please_input_subject'));
		xn_strlen($subject) > 128 AND message('subject', lang('subject_length_over_limit', array('maxlength' => 128)));

		$intro = param('intro');
		empty($intro) AND message('intro', "请输入简绍");
		xn_strlen($intro) > 255 AND message('intro', '介绍太长了', array('maxlength' => 255));

		$message = param('message', '', FALSE);
		empty($message) AND message('message', lang('please_input_message'));
		$doctype = param('doctype', 0);
		$doctype > 10 AND message(-1, lang('doc_type_not_supported'));
		xn_strlen($message) > 2028000 AND message('message', lang('message_too_long'));

		$info_gsmc = param('info_gsmc','');
		$info_xxdz = param('info_xxdz','');
		$info_lxr = param('info_lxr');
		empty($info_lxr) AND message('info_lxr', '没有联系人');
		$info_lxdh = param('info_lxdh');
		empty($info_lxdh) AND message('info_lxdh', '没有联系电话');
		$info_wxh = param('info_wxh','');

		$r = user_thread_quota_set($uid,-1);

		if ($r == FALSE) {
			message(-1,'发帖失败');
		}

		$thread = array(
			'fid' => $fid,
			'uid' => $uid,
			'sid' => $sid,
			'cid' => $cid,
			'subject' => $subject,
			'intro' => $intro,
			'info' => array(
				'info_gsmc' => $info_gsmc,
				'info_xxdz' => $info_xxdz,
				'info_lxr' => $info_lxr,
				'info_lxdh' => $info_lxdh,
				'info_wxh' => $info_wxh,
			),
			'message' => $message,
			'time' => $time,
			'longip' => $longip,
			'doctype' => $doctype,
		);

		//print_r($thread);
		// hook thread_create_thread_before.php

		$tid = thread_create($thread, $pid);
		$pid === FALSE AND message(-1, lang('create_post_failed'));
		$tid === FALSE AND message(-1, lang('create_thread_failed'));

		
		

		// hook thread_create_thread_end.php
		message(0, lang('create_thread_sucessfully'));

	}
} elseif ($action == 'upload') {
	if ($_FILES["file"]["error"] > 0) {
		$result = array(
			'errno' => 1,
			'data' => array(),
		);
	} else {
		$name = $_FILES["file"]["name"];
		$ext = file_ext($name, 7);
		$filetypes = include APP_PATH . 'conf/attach.conf.php';
		!in_array($ext, $filetypes['all']) AND $ext = '_' . $ext;
		$tmpanme = $uid . '_' . xn_rand(15) . '.' . $ext;
		$tmpfile = $conf['upload_path'] . 'tmp/' . $tmpanme;
		$tmpurl = $conf['upload_url'] . 'tmp/' . $tmpanme;
		$filetype = attach_type($name, $filetypes);
		move_uploaded_file($_FILES["file"]["tmp_name"], $tmpfile);
		$dst_path = $tmpfile;
		$src_path = APP_PATH."view/img/Logo.jpg";
		//创建图片的实例
		$dst = imagecreatefromstring(file_get_contents($dst_path));
		$src = imagecreatefromstring(file_get_contents($src_path));
		//获取水印图片的宽高
		list($src_w, $src_h) = getimagesize($src_path);
		//将水印图片复制到目标图片上，最后个参数50是设置透明度，这里实现半透明效果
		imagecopymerge($dst, $src, 10, 10, 0, 0, $src_w, $src_h, 30);
		//如果水印图片本身带透明色，则使用imagecopy方法
		//imagecopy($dst, $src, 10, 10, 0, 0, $src_w, $src_h);
		//输出图片
		list($dst_w, $dst_h, $dst_type) = getimagesize($dst_path);
		switch ($dst_type) {
			case 1://GIF
				imagegif($dst,$dst_path);
				break;
			case 2://JPG
				imagejpeg($dst,$dst_path);
				break;
			case 3://PNG
				imagepng($dst,$dst_path);
				break;
			default:
				break;
		}
		imagedestroy($dst);
		imagedestroy($src);
		sess_restart();
		empty($_SESSION['tmp_files']) AND $_SESSION['tmp_files'] = array();
		$n = count($_SESSION['tmp_files']);
		$filesize = filesize($tmpfile);
		$attach = array(
			'url' => $tmpurl,
			'path' => $tmpfile,
			'orgfilename' => $name,
			'filetype' => $filetype,
			'filesize' => $filesize,
			'width' => 0,
			'height' => 0,
			'isimage' => 1,
			'downloads' => 0,
			'aid' => '_' . $n,
		);
		$_SESSION['tmp_files'][$n] = $attach;
		unset($attach['path']);
		$result = array(
			'errno' => 0,
			'data' => array($tmpurl),
		);
	}
	die(json_encode($result));
// 帖子详情 | post detail
} else {
	// thread-{tid}-{page}-{keyword}.htm
	$tid = param(1, 0);
	$page = param(2, 1);
	$keyword = param(3);
	$pagesize = $conf['postlist_pagesize'];
	//$pagesize = 10;
	//$page == 1 AND $pagesize++;

	// hook thread_info_start.php

	$thread = thread_read($tid);
	empty($thread) AND message(-1, lang('thread_not_exists'));

	$fid = $thread['fid'];
	$forum = forum_read($fid);
	empty($forum) AND message(3, lang('forum_not_exists'));

	$sid = $thread['sid'];
	$sort = sort_read($sid);

	$cid = $thread['cid'];
	$city = city_read_cache($cid);

	$thread['info'] = xn_json_decode($thread['info']);

	$postlist = post_find_by_tid($tid, $page, $pagesize);
	empty($postlist) AND message(4, lang('post_not_exists'));

	if ($page == 1) {
		empty($postlist[$thread['firstpid']]) AND message(-1, lang('data_malformation'));
		$first = $postlist[$thread['firstpid']];
		unset($postlist[$thread['firstpid']]);
		$attachlist = $imagelist = $filelist = array();

		// 如果是大站，可以用单独的点击服务，减少 db 压力
		// if request is huge, separate it from mysql server
		thread_inc_views($tid);
	} else {
		$first = post_read($thread['firstpid']);
	}

	$keywordurl = '';
	if ($keyword) {
		$thread['subject'] = post_highlight_keyword($thread['subject'], $keyword);
		//$first['message'] = post_highlight_keyword($first['subject']);
		$keywordurl = "-$keyword";
	}
	$allowpost = forum_access_user($fid, $gid, 'allowpost') ? 1 : 0;
	$allowupdate = forum_access_mod($fid, $gid, 'allowupdate') ? 1 : 0;
	$allowdelete = forum_access_mod($fid, $gid, 'allowdelete') ? 1 : 0;

	forum_access_user($fid, $gid, 'allowread') OR message(-1, lang('user_group_insufficient_privilege'));

	$pagination = pagination(url("thread-$tid-{page}$keywordurl"), $thread['posts'] + 1, $page, $pagesize);

	$header['title'] = $thread['subject'] . '-' . $forum['name'] . '-' . $conf['sitename'];
	//$header['mobile_title'] = lang('thread_detail');
	$header['mobile_title'] = $forum['name'];
	$header['mobile_link'] = url("forum-$fid");
	$header['keywords'] = '';
	$header['description'] = $thread['subject'];
	$_SESSION['fid'] = $fid;

	// hook thread_info_end.php

	include _include(APP_PATH . 'view/htm/thread.htm');
}

// hook thread_end.php

?>